﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tao.OpenGl;
using Tao.FreeGlut;
using Tao.Platform.Windows;

namespace Redaktor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            simpleOpenGlControl1.InitializeContexts();
        }

        int width1, width2, width3;
        int height1, height2, height3;
        string nameButton, nameForm, nameTextBox;
        int numButton = 1, numForm = 1, numTextBox = 1;

        private void Form1_Load(object sender, EventArgs e)
        { 
            Glut.glutInit();
            Glut.glutInitDisplayMode(Glut.GLUT_RGB | Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH); 
            Gl.glClearColor(255, 255, 255, 1);
            Gl.glViewport(0, 0, simpleOpenGlControl1.Width, simpleOpenGlControl1.Height);
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            if (simpleOpenGlControl1.Width <= simpleOpenGlControl1.Height)
                Glu.gluOrtho2D(0.0, 30.0, 0.0, 30.0 * (float)simpleOpenGlControl1.Height / (float)simpleOpenGlControl1.Width);
            else
                Glu.gluOrtho2D(0.0, 30.0 * (float)simpleOpenGlControl1.Width / (float)simpleOpenGlControl1.Height, 0.0, 30.0);

            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Button_Click(object sender, EventArgs e)
        {
            width1 = Convert.ToInt32(textBox1.Text);
            height1 = Convert.ToInt32(textBox2.Text);
            Draw_Button();
            numButton=numButton + 1;
        }
        private void Form_Click(object sender, EventArgs e)
        {
            width2 = Convert.ToInt32(textBox3.Text);
            height2 = Convert.ToInt32(textBox4.Text);
            Draw_Form();
            numForm = numForm + 1;
        }
        private void TextBox_Click(object sender, EventArgs e)
        {
            width3 = Convert.ToInt32(textBox5.Text);
            height3 = Convert.ToInt32(textBox6.Text);
            Draw_TextBox();
            numTextBox = numTextBox + 1;
        }
        private void Draw_Button()
        {
            //Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3d(0.0,1.0,1.0);
            Gl.glVertex2i(5, 5);
            Gl.glVertex2i(5, 5 + height1);
            Gl.glVertex2i(5 + width1, 5 + height1);
            Gl.glVertex2i(5 + width1, 5);
            Gl.glEnd();

            Gl.glColor3d(0.0, 1.0, 0.0);
            //левая рамка
            Gl.glVertex2i(5, 5);
            Gl.glVertex2i(4, 5);
            Gl.glVertex2i(4, 5 + height1);
            Gl.glVertex2i(5, 5 + height1);
            //нижняя рамка
            Gl.glVertex2i(4, 4);
            Gl.glVertex2i(4, 5);
            Gl.glVertex2i(6 + width1, 5);
            Gl.glVertex2i(6 + width1, 4);
            //правая рамка
            Gl.glVertex2i(6 + width1, 5);
            Gl.glVertex2i(5 + width1, 5);
            Gl.glVertex2i(5 + width1, 5 + height1);
            Gl.glVertex2i(6 + width1, 5 + height1);        
            //верхняя рамка
            Gl.glVertex2i(4, 6 + height1);
            Gl.glVertex2i(4, 5 + height1);
            Gl.glVertex2i(6 + width1, 5 + height1);
            Gl.glVertex2i(6 + width1, 6 + height1);
            Gl.glEnd();

            //название
            Gl.glColor3d(0.0, 0.0, 0.0);
            nameButton = "Button" + numButton;
            PrintText2D((float)(7.2+width1)/2, (float)(9.5+height1)/2, nameButton);
            Gl.glFlush();
            simpleOpenGlControl1.Invalidate();
        }

        private void Draw_Form()
        {
            //Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3d(0.0, 0.6, 0.5);
            Gl.glVertex2i(0, 30);
            Gl.glVertex2i(0, 30 - height2);
            Gl.glVertex2i(width2, 30 - height2);
            Gl.glVertex2i(width2, 30);
            Gl.glEnd();
            Gl.glFlush();

            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3d(0.2, 0.4, 0.6);
            //верхняя рамка
            Gl.glVertex2i(0, 30);
            Gl.glVertex2i(0, 28);
            Gl.glVertex2i(width2, 28);
            Gl.glVertex2i(width2, 30);
            //левая рамка
            Gl.glVertex2i(0, 30);
            Gl.glVertex2i(0, 30 - height2);
            Gl.glVertex2i(1, 30 - height2);
            Gl.glVertex2i(1, 30);
            //правая рамка
            Gl.glVertex2i(width2, 30);
            Gl.glVertex2i(width2, 30 - height2);
            Gl.glVertex2i(width2 - 1, 30 - height2);
            Gl.glVertex2i(width2 - 1, 30);
            Gl.glEnd();

            //название
            Gl.glColor3d(0.0, 0.0, 0.0);
            nameForm = "Form" + numForm;
            PrintText2D((float)1, (float)29, nameForm);

            Gl.glFlush();
            simpleOpenGlControl1.Invalidate();
        }
        private void Draw_TextBox()
        {
            //Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glColor3d(1.0, 1.0, 1.0);
            Gl.glVertex2i(15, 5);
            Gl.glVertex2i(15, 5 + height3);
            Gl.glVertex2i(15 + width3, 5 + height3);
            Gl.glVertex2i(15 + width3, 5);
            Gl.glEnd();

            //название
            Gl.glColor3d(0.0, 0.0, 0.0);
            nameTextBox = "TextBox"+numTextBox;
            PrintText2D((float)16, (float)(4+height3), nameTextBox);
            Gl.glFlush();
            simpleOpenGlControl1.Invalidate();
        }

        // фукнция визуализации текста 
        private void PrintText2D(float x, float y, string text)
        {
            // устанавливаем позицию вывода растровых символов  
            // в переданных координатах x и y. 
            Gl.glRasterPos2f(x, y);

            // в цикле foreach перебираем значения из массива text, 
            // который содержит значение строки для визуализации  
            foreach (char char_for_draw in text)
            {
                // визуализируем символ c, с помощью функции glutBitmapCharacter, используя шрифт GLUT_BITMAP_9_BY_15.  
                Glut.glutBitmapCharacter(Glut.GLUT_BITMAP_9_BY_15, char_for_draw);
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //Draw();
        }
    }
}

